package com.loop.unclelend.ui.presenter

import com.loop.unclelend.network.ApiService
import com.loop.unclelend.network.ApiService.addCommonParams
import com.loop.unclelend.network.ApiService.addRandomParams
import com.loop.unclelend.network.IHttpCallback
import com.loop.unclelend.pro.ProPresenter
import com.loop.unclelend.ui.view.IView_Loan_Status
import com.loop.unclelend.utils.helper.CustomConfigsHelper
import com.loop.unclelend.utils.helper.CustomProcessHelper
import com.loop.unclelend.utils.helper.UncleStatusHelper
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/27 15:38
 * @Annotation：审核中、放款中、已拒绝
 */
class Pre_Loan_Status(private val iView: IView_Loan_Status): ProPresenter(iView) {

    /**
     * 获取状态
     */
    fun checkUncleStatusAction(){
        requestDetailsAction()
        //申请状态
        val applyStatus = CustomProcessHelper.getUncleApplyStatus()
        //订单状态
        val orderStatus = CustomProcessHelper.getUncleOrderStatus()

        val isReviewing = UncleStatusHelper.StatusRepository(applyStatus,orderStatus).isReviewing()
        val isDisbursing = UncleStatusHelper.StatusRepository(applyStatus,orderStatus).isDisbursing()
        val isRejected = UncleStatusHelper.StatusRepository(applyStatus,orderStatus).isRejected()
        val isRepay = UncleStatusHelper.StatusRepository(applyStatus,orderStatus).isRepayNow()
        //审核中
        if (isReviewing){
            iView.showStatusAuditViews()
            return
        }
        //放款中
        if (isDisbursing){
            iView.showStatusLendingViews()
            return
        }
        //已拒绝
        iView.showStatusRejectedViews()
    }

    /**
     * 获取贷款信息
     */
    private fun requestDetailsAction(){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.UNCLE_API_COMMON_URL)
        val params = JSONObject().addCommonParams().addRandomParams().apply {
            put("path",ApiService.API_PATH.UNCLE_API_LOAN_APPLY_PATH)
        }
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    //显示共用View
                    iView.showStatusCommonViews(obj)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                iView.hideUncleLoading()
            }
        })
    }

    /**
     * 获取账户列表
     */
    fun requestAccountListAction(){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.UNCLE_API_COMMON_URL)
        val params = JSONObject().addCommonParams().addRandomParams().apply {
            put("path", ApiService.API_PATH.UNCLE_API_ACCOUNT_LIST_PATH)
        }
        doPostAction(httpUrl, params, object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val array = data.optJSONArray("obj")
                val accountList = arrayListOf<JSONObject>()

                if (array != null && array.length() > 0) {
                    for (i in 0..<array.length()) {
                        val item = array.optJSONObject(i)
                        item.put("isEdit", false)
                        accountList.add(item)
                    }
                }
                iView.showAccountList(accountList)
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {

            }
        })
    }

    /**
     * 添加绑卡
     */
    fun requestBindAccountAction(mobile: String, walletChannel: String, bindFlag:String = "NO"){
        iView.showUncleLoading()
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.UNCLE_API_COMMON_URL)
        val params = JSONObject().addCommonParams().addRandomParams().apply {
            put("path", ApiService.API_PATH.UNCLE_API_ACCOUNT_ADD_PATH)
            put("walletMobile", mobile)
            put("walletMobileConfirm", mobile)
            put("walletChannel", walletChannel)
            put("addBindFlag", bindFlag)
        }

        doPostAction(httpUrl, params, object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                //刷新列表
                requestAccountListAction()
                val obj = data.optJSONObject("obj") ?: JSONObject()

                // "YES/NO 是否需要提示用户这个号码可以绑另一个渠道的钱包"
                val addBindFlag = obj.optString("addBindFlag")
                if (addBindFlag == "YES") {
                    //需要提示绑定另一个渠道账户
                    var otherAccountInfo: JSONObject? = null

                    //用于从APP配置中获取（walletChannels）对应的信息
                    val channel = obj.optString("channel")
                    if (channel.isNotBlank()) {
                        val customize = CustomConfigsHelper.getUncleConfigData()
                        if (customize != null) {
                            val walletChannels = customize.optJSONArray("walletChannels")
                            if (walletChannels != null && walletChannels.length() > 0) {
                                for (i in 0..<walletChannels.length()) {
                                    val item = walletChannels.optJSONObject(i)
                                    if (channel == item.optString("value")) {
                                        otherAccountInfo = item
                                        break
                                    }
                                }
                            }

                            // 只有非空的情况下才处理
                            if (otherAccountInfo != null) {
                                otherAccountInfo.put("walletMobile", mobile)
                                //提示绑定另一个渠道账户
                                iView.showBindingAccountOtherDialog(otherAccountInfo)
                                return
                            }
                        }
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 删除账户
     */
    fun requestDeleteAccountAction(ids: String) {
        iView.showUncleLoading()
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.UNCLE_API_COMMON_URL)
        val params = JSONObject().addCommonParams().addRandomParams().apply {
            put("path", ApiService.API_PATH.UNCLE_API_ACCOUNT_DELETE_PATH)
            put("ids", ids)
        }
        doPostAction(httpUrl, params, object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.showUncleToast(data.optString("msg"))
                //刷新列表
                requestAccountListAction()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }
}